package leetcode;

import java.util.Stack;

public class LeetCode20 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                if (stack.size() == 0)
                    return false;
                Character pop = stack.pop();
                if (getDict(pop) != c)
                    return false;
            }
        }
        return stack.size() == 0;
    }

    public Character getDict(char c) {
        switch (c) {
            case '(':
                return ')';
            case '{':
                return '}';
            case '[':
                return ']';
            default:
                return 0;
        }
    }
}
